package com.stkouyu;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.stkouyu.listener.OnLancooSpeechRecognitionListener;
import com.stkouyu.xiansheng.SSoundManager;
import com.stkouyu.xiansheng.entity.ResultDetails;
import com.stkouyu.xiansheng.entity.SoundResult;

import java.lang.ref.WeakReference;
import java.util.List;


/**
 * Created by pengmi on 2018/11/21.
 * Description:语音识别
 */

public class LancooFileRecognitionManager {

    private static LancooFileRecognitionManager lancooSpeechRecognitionManager;
    private Context mContext;
    private WeakReference<OnLancooSpeechRecognitionListener> onLancooSpeechRecognitionListener;
    private final int MST_OUTTIME=10;

    private LancooFileRecognitionManager(Context mContext){
        this.mContext=mContext.getApplicationContext();
    }

    public static LancooFileRecognitionManager getInstance(Context mContext){
        if(lancooSpeechRecognitionManager==null){
            lancooSpeechRecognitionManager=new LancooFileRecognitionManager(mContext);
        }
        return lancooSpeechRecognitionManager;
    }

    public void cancelRecognition(Context mContext){
        SSoundManager.getInstance(mContext).cancelRecord();
    }


    public void startFileRecognition(String filePath,OnLancooSpeechRecognitionListener onLancooSpeechRecognitionListener){
        startFileRecognition(filePath,0,onLancooSpeechRecognitionListener);


    }

    public void startFileRecognition(String url,int recognitionType,
                                     OnLancooSpeechRecognitionListener onLancooSpeechRecognitionListener){
        if(TextUtils.isEmpty(url)){
            return;
        }
        SSoundManager.getInstance(mContext).startFileImportRecognition(url,recognitionType,mHandler,onLancooSpeechRecognitionListener);
        this.onLancooSpeechRecognitionListener=new WeakReference<>(onLancooSpeechRecognitionListener);
        mHandler.sendEmptyMessageDelayed(MST_OUTTIME,15000);
    }


    private Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg ) {
            mHandler.removeMessages(MST_OUTTIME);
            if(msg.what==MST_OUTTIME){
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecognitionError("out time!");
                }
                if(mContext!=null){
                    SSoundManager.getInstance(mContext).cancelRecord();
                    LancooSkEgnManager.getInstance(mContext).initSkegn(mContext);
                    Log.e("aaaa","重新进行评测初始化。。。");
                }
            }else if(msg.what==101){
                String error= (String) msg.obj;
                Log.e("123","error:"+error);

                int arg1 = msg.arg1;
                if((arg1==41030||arg1==70017)&&mContext!=null){
                    LancooSkEgnManager.getInstance(mContext).initSkegn(mContext);
                }
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecognitionError(arg1+"");
                }
            }else if(msg.what==100){
                SoundResult soundResult= (SoundResult) msg.obj;
                List<ResultDetails> wrd_details = soundResult.getWrd_details();
                StringBuilder stringBuilder=new StringBuilder();
                if(wrd_details!=null&&wrd_details.size()>0){
                    for (ResultDetails resultDetails:wrd_details){
                        stringBuilder.append(resultDetails.getText());
                        if(soundResult.getRecognitionType()==0){
                            stringBuilder.append(" ");
                        }
                    }
                }
                Log.e("123","error:"+stringBuilder.toString());
                mHandler.removeMessages(MST_OUTTIME);
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecognitionEnd(stringBuilder.toString(),soundResult.getRecordUrl());
                }
            }else if(msg.what==104){
                SoundResult soundResult= (SoundResult) msg.obj;
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecognitionEnd("",soundResult.getRecordUrl());
                }
            }else if(msg.what==105){
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecognitionStart();
                }
            }else if(msg.what==110){
                if(onLancooSpeechRecognitionListener!=null&&onLancooSpeechRecognitionListener.get()!=null){
                    onLancooSpeechRecognitionListener.get().onSpeechRecogniting(0,msg.arg1);
                }
            }
            return false;
        }
    });


}
